<?php

    /**
	 * XHTML form components helper class. Used for xHTML/HTML5 compatible form components generation
	 * 
	 * @package Helpers
	 * @author Perkin Anton
	 * @version 1.0
     */
    class HtmlFormHelper extends HtmlHelper
    {
        /* VARIABLES */
        
		/**
		 * default field block template
		 * @var string
		 */
		private static $defaultFieldBlockTemplate = '<div><label>{label}</label>: <span>{field}</span> <span>{error}</span> <span>{comment}</span></div>';
        
		
		/* PROPERTIES */
		
    	/**
    	 * @return the $defaultFieldBlockTemplate
    	 */
    	public static function getDefaultFieldBlockTemplate ()
    	{
    		return self::$defaultFieldBlockTemplate;
    	}
    
    	/**
    	 * @param $defaultFieldBlockTemplate the $defaultFieldBlockTemplate to set
    	 */
    	public static function setDefaultFieldBlockTemplate ($defaultFieldBlockTemplate)
    	{
    		self::$defaultFieldBlockTemplate = $defaultFieldBlockTemplate;
    	}	
		
        
		/* FORM BLOCKS */
		
		/**
		 * Displays input text field with label and other features
		 * 
		 * @param $name
		 * @param $value
		 * @param $label
		 * @param $settings
		 * @param $template
		 */
		public static function InputTextBlock($name, $value, $label = '', $settings = array(), $template = '')
		{
			//input attributes
			$fieldAttributes = (empty($settings['field_attributes'])) ? '' : $settings['field_attributes'];
			$fieldAttributes = self::GetAttributesString(array('name' => $name, 'value' => $value, 'type' => 'text')).self::GetAttributesString($fieldAttributes);
			
			$htmlBlock = self::GetCommonInputFieldBlock($name, $value, $label, $settings, $template, self::$defaultFieldBlockTemplate);
			
			//inserts field into template
			$htmlBlock = str_replace('{field}', self::GetTagRaw('input', $fieldAttributes), $htmlBlock);
			
			//displays the block
			echo $htmlBlock;
		}

		/**
		 * Displays input password field with label and other features
		 * 
		 * @param $name
		 * @param $value
		 * @param $label
		 * @param $settings
		 * @param $template
		 */
		public static function InputPasswordBlock($name, $value, $label = '', $settings = array(), $template = '')
		{
			//input attributes
			$fieldAttributes = (empty($settings['field_attributes'])) ? '' : $settings['field_attributes'];
			$fieldAttributes = self::GetAttributesString(array('name' => $name, 'value' => $value, 'type' => 'password')).self::GetAttributesString($fieldAttributes);
			
			$htmlBlock = self::GetCommonInputFieldBlock($name, $value, $label, $settings, $template, self::$defaultFieldBlockTemplate);
			
			//inserts field into template
			$htmlBlock = str_replace('{field}', self::GetTagRaw('input', $fieldAttributes), $htmlBlock);
			
			//displays the block
			echo $htmlBlock;
		}		
		
		/**
		 * Displays input text field with label and other features
		 * 
		 * @param $name
		 * @param $value
		 * @param $label
		 * @param $settings
		 * @param $template
		 */
		public static function TextareaBlock($name, $value, $label = '', $settings = array(), $template = '')
		{
			//input attributes
			$fieldAttributes = (empty($settings['field_attributes'])) ? '' : $settings['field_attributes'];
			$fieldAttributes = self::GetAttributeString('name', $name).self::GetAttributesString($fieldAttributes);
			
			$htmlBlock = self::GetCommonInputFieldBlock($name, $value, $label, $settings, $template, self::$defaultFieldBlockTemplate);
			
			//inserts field into template
			$htmlBlock = str_replace('{field}', self::GetTagRaw('textarea', $fieldAttributes, $value), $htmlBlock);
			
			//displays the block
			echo $htmlBlock;			
		}		
		
		/**
		 * Displays select box field with label and other features
		 * 
		 * @param array $name
		 * @param $options
		 * @param $value
		 * @param $label
		 * @param $settings
		 * @param $template
		 */
		public static function SelectBlock($name, array $options = array(), $value = '', $label = '', $settings = array(), $template = '')
		{
			//input attributes
			$fieldAttributes = (empty($settings['field_attributes'])) ? '' : $settings['field_attributes'];
			$fieldAttributes = self::GetAttributeString('name', $name).self::GetAttributesString($fieldAttributes);
			
			//empty option for the select box
			$settings['empty_option_value'] = (!isset($settings['empty_option_value'])) ? '' : $settings['empty_option_value'];
			$settings['empty_option_text'] = (!isset($settings['empty_option_text'])) ? '-' : $settings['empty_option_text'];
			
			$htmlBlock = self::GetCommonInputFieldBlock($name, $value, $label, $settings, $template, self::$defaultFieldBlockTemplate);
			
			//gets the select tag contents
			ob_start();
			self::Select($name, $options, $value, $settings['empty_option_value'], $settings['empty_option_text'], $fieldAttributes);
			$field = ob_get_contents();
			ob_end_clean();
			
			//inserts field into template
			$htmlBlock = str_replace('{field}', $field, $htmlBlock);
			
			//displays the block
			echo $htmlBlock;
		}
		
		/**
		 * Displays submit button within template
		 * 
		 * @param $name
		 * @param $value
		 * @param $settings
		 * @param $template
		 */
		public static function InputSubmitBlock($name, $value, $settings = array(), $template = '')
		{
			//default template
			$defaultTemplate = '<span>{field}</span>';
			
			//submit type
			$type = (isset($settings['field_attributes']['type'])) ? $settings['field_attributes']['type'] : 'submit';
			
			//input attributes
			$fieldAttributes = (empty($settings['field_attributes'])) ? '' : $settings['field_attributes'];
			$fieldAttributes = self::GetAttributesString(array('name' => $name, 'value' => $value, 'type' => $type)).self::GetAttributesString($fieldAttributes);
			
			$htmlBlock = self::GetCommonInputFieldBlock($name, $value, '', $settings, $template, $defaultTemplate);
			
			//inserts field into template
			$htmlBlock = str_replace('{field}', self::GetTagRaw('input', $fieldAttributes), $htmlBlock);
			
			//displays the block
			echo $htmlBlock;			
		}
		
		/**
		 * Gets the common HTML output for the input field blocks
		 * 
		 * @param $name
		 * @param $value
		 * @param $label
		 * @param $settings
		 * @param $template
		 * @param $defaultTemplate
		 * @return string
		 */
		private static function GetCommonInputFieldBlock($name, $value, $label, $settings = array(), $template, $defaultTemplate)
		{			
			//input error
			$inputError = (empty($settings['error'])) ? '' : $settings['error'];
			
			//input comment
			$inputComment = (empty($settings['comment'])) ? '' : $settings['comment'];
			
			//label attributes
			$labelAttributes = (empty($settings['label_attributes'])) ? '' : $settings['label_attributes'];
			
			//xhtml template
			$template = (empty($template)) ? $defaultTemplate : $template;

			//inserts label into template
			$htmlBlock = str_replace('{label}', $label, $template);
			//$htmlBlock = str_replace('{label}', self::GetLabel($name, $label, $labelAttributes), $template);
			
			//inserts input error into template
			$htmlBlock = str_replace('{error}', $inputError, $htmlBlock);

			//inserts input error into template
			$htmlBlock = str_replace('{comment}', $inputComment, $htmlBlock);
			
			return $htmlBlock;
		}

		/**
		 * Displays input checkbox field with label and other features
		 * 
		 * @param $name
		 * @param $value
		 * @param $label
		 * @param $settings
		 * @param $template
		 */
		public static function InputCheckboxBlock($name, $isChecked, $label = '', $value, $settings = array(), $template = '')
		{
			//input attributes
			$fieldAttributes = (empty($settings['field_attributes'])) ? '' : $settings['field_attributes'];
			$checkedAttribute = ($isChecked) ? self::GetAttributeString('checked', 'checked') : "";
			$fieldAttributes = self::GetAttributesString(array('name' => $name, 'value' => $value, 'type' => InputType::Checkbox)).' '.$checkedAttribute.self::GetAttributesString($fieldAttributes);

			$htmlBlock = self::GetCommonInputFieldBlock($name, $value, $label, $settings, $template, self::$defaultFieldBlockTemplate);
			
			//inserts field into template
			$htmlBlock = str_replace('{field}', self::GetTagRaw('input', 'type = "'.InputType::Hidden.'" name = "'.$name.'" value = "0"').self::GetTagRaw('input', $fieldAttributes), $htmlBlock);

			//displays the block
			echo $htmlBlock;
		}
		
		/**
		 * Gets 'label' tag contents
		 * 
		 * @param mixed $elementId value of the 'id' attribute
		 * @param string $content label
		 * @param string|array $attributes tag attributes
		 * 
		 * @return string
		 */
		public static function GetLabel($elementId, $content, $attributes = '')
		{
			$attributes = self::GetAttributeString('for', $elementId).self::GetAttributesString($attributes);
			
			return self::GetTagRaw('label', $attributes, $content);
		}
		
    	/**
		 * Generates form with all the fields included.
		 * TODO: to finish
		 * 
		 * @param $action
		 * @param $elements
		 * @param $attributes
		 * @param $template
		 */
		public static function FormObject($action, $elements, $attributes = '', $template = array())
		{
			//form attributes string
			$attributes = self::GetAttributeString('action', $action).self::GetAttributesString($attributes);
			
			$content = $template['header'];
			
			foreach ($elements as $element)
			{
				//element id
				$elementId = (empty($element['attributes']['id'])) ? $element['name'] : $element['attributes']['id'];
				
				//element attributes
				$elementAttributes = "name = '{$element['name']}' value = '{$element['value']}' type = '{$element['type']}' id = '$elementId' ".self::GetAttributesString($element['attributes']);
				
				//template
				$elementTemplate = (empty($element['template'])) ? $template['element'] : $element['template'];	
				
				//adds the current element to the form content
				if (!empty($elementTemplate))
				{
					//parses the template
					$elementContent = str_replace('{label}', self::GetLabel($elementId, $element['label']), $elementTemplate);
					$elementContent = str_replace('{field}', self::GetTag($element['tag'], $elementAttributes), $elementContent);
					
					$content .= $elementContent;
				}
				else
				{
					$content .= self::GetLabel($elementId, $element['label']).self::GetTag($element['tag'], $elementAttributes);
				}
			}
			
			$content .= $template['footer'];
			
			self::TagRaw('form', $attributes, $content);
		}		
		
    }

?>